<template>
  <div class="page-delivery">
    <div class="search-bar">
      <el-form
        ref="searchFormRef"
        :inline="true"
        class="search-form"
        :model="searchForm"
        @submit.prevent
      >
        <div class="input-row">
          <el-form-item
            prop="dvyName"
            :label="$t('order.logisticsCompanyName') + ':'"
          >
            <el-input
              v-model="searchForm.dvyName"
              type="text"
              clearable
              :placeholder="$t('order.logisticsCompanyName')"
            />
          </el-form-item>
          <el-form-item
            prop="dvyNo"
            :label="
              $t('platform.logisticsCompanyNumber') + `（${$t('sysManagement.expressBird')}）` + ':'
            "
          >
            <el-input
              v-model="searchForm.dvyNo"
              type="text"
              clearable
              :placeholder="
                $t('platform.logisticsCompanyNumber') + `（${$t('sysManagement.expressBird')}）`
              "
            />
          </el-form-item>
          <el-form-item
            prop="dvyNoHd"
            :label="
              $t('platform.logisticsCompanyNumber') + `（${$t('sysManagement.quick100')}）` + ':'
            "
          >
            <el-input
              v-model="searchForm.dvyNoHd"
              type="text"
              clearable
              :placeholder="
                $t('platform.logisticsCompanyNumber') + `（${$t('sysManagement.quick100')}）`
              "
            />
          </el-form-item>
          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="onSearch(true)"
            >
              {{ $t('crud.searchBtn') }}
            </div>
            <div
              class="default-btn"
              @click="onResetSearch(searchFormRef)"
            >
              {{ $t('crud.emptyBtn') }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <div class="main-container">
      <div class="operation-bar">
        <div
          v-if="isAuth('admin:delivery:save')"
          class="default-btn primary-btn"
          @click="onAddOrUpdate()"
        >
          {{ $t('crud.addTitle') }}
        </div>
      </div>
      <div class="table-con">
        <el-table
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row-low"
          style="width: 100%"
        >
          <el-table-column
            :label="$t('number')"
            type="index"
            align="left"
            width="100"
          />
          <el-table-column
            align="left"
            prop="dvyName"
            :label="$t('order.logisticsCompanyName')"
          >
            <template #default="scope">
              <span class="table-cell-text line-clamp-one">{{ scope.row.dvyName || '-' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="left"
            prop="companyHomeUrl"
            :label="$t('platform.companyHomepage')"
          >
            <template #default="scope">
              <span class="table-cell-text line-clamp-one">{{ scope.row.companyHomeUrl || '-' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="left"
            prop="recTime"
            width="160"
            :label="$t('sys.creationTime')"
          >
            <template #default="scope">
              <span class="table-cell-text">{{ scope.row.recTime || '-' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="left"
            prop="modifyTime"
            width="160"
            :label="$t('platform.updateDate')"
          >
            <template #default="scope">
              <span class="table-cell-text">{{ scope.row.modifyTime || '-' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="left"
            prop="dvyNo"
            width="190"
            :label="
              $t('platform.logisticsCompanyNumber') + `（${$t('sysManagement.expressBird')}）`
            "
          >
            <template #default="scope">
              <span class="table-cell-text">{{ scope.row.dvyNo || '-' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="left"
            prop="dvyNoHd"
            width="190"
            :label="$t('platform.logisticsCompanyNumber') + `（${$t('sysManagement.quick100')}）`"
          >
            <template #default="scope">
              <span class="table-cell-text">{{ scope.row.dvyNoHd || '-' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="left"
            prop="aliNo"
            width="160"
            :label="$t('platform.logisticsCompanyNumber') + `（${$t('sysManagement.quickAli')}）`"
          >
            <template #default="scope">
              <span class="table-cell-text">{{ scope.row.aliNo || '-' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            :label="$t('crud.menu')"
            width="auto"
          >
            <template #default="scope">
              <div class="text-btn-con">
                <div
                  v-if="isAuth('admin:delivery:update')"
                  class="default-btn text-btn"
                  @click="onAddOrUpdate(scope.row.dvyId)"
                >
                  {{ $t('brand.revise') }}
                </div>
                <div
                  v-if="isAuth('admin:delivery:delete')"
                  class="default-btn text-btn"
                  @click="onDelete(scope.row.dvyId)"
                >
                  {{ $t('brand.delete') }}
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        :total="page.total"
        layout="total, sizes, prev, pager, next, jumper"
        @update:page-size="onPageSizeChange"
        @update:current-page="onPageChange"
      />
    </div>
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdateRef"
      @refresh-data-list="refreshChange"
    />
  </div>
</template>

<script setup>
import AddOrUpdate from './add-or-update.vue'
import { ElMessageBox, ElMessage } from 'element-plus'
import { isAuth } from '@/utils/index'

const page = ref({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
onMounted(() => {
  onGetDataList(page.value)
})
const dataList = ref([]) // 列表数据
let tempSearchForm = null // 保存上次点击查询的请求条件
const searchForm = ref({
  dvyName: '',
  dvyNo: '',
  dvyNoHd: ''
}) // 搜索
const onGetDataList = (pageParam, newData = false) => {
  if (page.value) {
    const size = Math.ceil(page.value.total / page.value.pageSize)
    page.value.currentPage = (page.value.currentPage > size ? size : page.value.currentPage) || 1
  }
  if (newData || !tempSearchForm) {
    tempSearchForm = JSON.parse(JSON.stringify(searchForm.value))
  }
  http({
    url: http.adornUrl('/admin/delivery/page'),
    method: 'get',
    params: http.adornParams(
      Object.assign(
        {
          current: pageParam == null ? page.value.currentPage : pageParam.currentPage,
          size: pageParam == null ? page.value.pageSize : pageParam.pageSize
        },
        tempSearchForm
      )
    )
  }).then(({ data }) => {
    dataList.value = data.records
    page.value.total = data.total
  })
}
// 新增 / 修改
const addOrUpdateVisible = ref(false)
const addOrUpdateRef = ref(null)

const onAddOrUpdate = id => {
  addOrUpdateVisible.value = true
  nextTick(() => {
    addOrUpdateRef.value?.init(id)
  })
}
const onDelete = id => {
  const ids = id ? [id] : dataList.value.map(item => {
    return item.prodId
  })
  ElMessageBox.confirm($t('admin.isDeleOper'), $t('remindPop.remind'), {
    confirmButtonText: $t('remindPop.confirm'),
    cancelButtonText: $t('remindPop.cancel'),
    type: 'warning'
  }).then(() => {
    http({
      url: http.adornUrl('/admin/delivery/' + id),
      method: 'delete',
      data: http.adornData({})
    }).then(() => {
      page.value.total = page.value.total - ids.length
      ElMessage({
        message: $t('publics.operation'),
        type: 'success',
        duration: 1500,
        onClose: () => {
          refreshChange()
        }
      })
    })
  })
}
/**
 * 刷新回调
 */
const refreshChange = () => {
  onGetDataList(page.value)
}
const onSearch = (newData = false) => {
  onGetDataList(page.value, newData)
}
const onPageSizeChange = val => {
  page.value.pageSize = val
  onGetDataList()
}
const onPageChange = val => {
  page.value.currentPage = val
  onGetDataList()
}
const searchFormRef = ref(null)
const onResetSearch = formName => {
  formName.resetFields()
}
</script>
